/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.admin.exam;

import hibernate.dao.CourseDAO;
import hibernate.dao.ExamDAO;
import hibernate.entity.Courses;
import hibernate.entity.ExamResult;
import hibernate.entity.ExamResultId;
import hibernate.entity.Exams;
import hibernate.entity.Students;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import ui.common.AbstractUIBean;
import util.Converter;

/**
 *
 * @author NDH
 */
@ManagedBean
@RequestScoped
public class ExamManager extends AbstractUIBean {

    /**
     * Creates a new instance of ExamManager
     */
    ExamDAO exDao = new ExamDAO();
    CourseDAO coDao = new CourseDAO();
    Courses courses;
    Exams exam;
    int orderNumber;
    int courseID;
    List<Exams> allExam = new ArrayList<Exams>();

    public ExamManager() {
        courses = new Courses();
        allExam = exDao.readLiving();
        exam = new Exams();
    }

    public int getCourseID() {
        return courseID;
    }

    public void setCourseID(int courseID) {
        this.courseID = courseID;
    }

    public int getOrderNumber() {
        return orderNumber;
    }

    public void setOrderNumber(int orderNumber) {
        this.orderNumber = orderNumber;
    }

    public Exams getExam() {
        return exam;
    }

    public void setExam(Exams exam) {
        this.exam = exam;
    }

    public Courses getCourses() {
        return courses;
    }

    public void setCourses(Courses courses) {
        this.courses = courses;
    }

    public List<Exams> getAllExam() {
        return allExam;
    }

    /**
     * @return String
     * @see list page with the exam which has already been created
     */
    public String docreate() {
        courses = new CourseDAO().readByID(courseID);
        exam = new Exams(courses, orderNumber);
        try {
            hbTransaction = hbSession.beginTransaction();
            exam.setStatus(true);
            hbSession.save(exam);
            Iterator<Students> listS = exam.getCourses().getStudentses().iterator();
            while (listS.hasNext()) {
                Students students = listS.next();
                ExamResult es = new ExamResult(exam, students);
                ExamResultId id = new ExamResultId(exam.getExamId(), students.getStudentId());
                es.setId(id);
                hbSession.save(es);
                int n = es.getExams().getExamId();
                n = es.getStudents().getStudentId();
                exam.getExamResults().add(es);
                exam = (Exams) hbSession.merge(exam);
                hbSession.update(exam);
                es = (ExamResult) hbSession.merge(es);
                hbSession.update(es);

            }
            hbTransaction.commit();
            allExam = exDao.readLiving();

            return "exam-list";
        } catch (Exception e) {
            hbTransaction.rollback();
        }
        return null;
    }

    /**
     * @return String
     * @see list page of exam with edited and updated one
     */
    public String doupdate() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        Exams ex = (Exams) session.getAttribute("editExam");
        ex.setExamOrderNumber(orderNumber);
        ex.setCourses(new CourseDAO().readByID(courseID));
        if (exDao.update(ex) > 0) {
            showPrimeMessage("Success", "Exam " + exam.getExamOrderNumber() + " updated");
            return "list?faces-redirect=true";
        }
        showPrimeMessage("Error", "Unexpected error! update fail");
        return null;
    }

    /**
     * @param Exams an exam for modifying its fields
     * @return String
     * @see edit page
     */
    public String edit(Exams c) {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        session.setAttribute("editExam", c);
        courseID = c.getCourses().getCourseId();
        try {
            orderNumber = c.getExamOrderNumber();
        } catch (Exception e) {
            orderNumber = 1;
        }
        return "edit";
    }

    /**
     * @param Exams an exam for modifying fields
     * @return String
     * @see editresult page is displayed
     */
    public String editResult(Exams c) {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        session.setAttribute("currExam", c);

        return "editresult";
    }

    /**
     * @return String
     * @see list of exams after updating overall
     */
    public String updateAll() {
        try {
            hbTransaction = hbSession.beginTransaction();
            List<ExamResult> allResult = Converter.iteratorToList(exam.getExamResults().iterator());
            for (ExamResult ex : allResult) {
                ex = (ExamResult) hbSession.merge(ex);
                hbSession.update(ex);
            }
            hbSession.update(exam);
            hbTransaction.commit();
            return "list";
        } catch (Exception e) {
            return null;
        }

    }

    /**
     * @param Exams an exam for removing from the system
     * @return String
     * @see list of exam, except removed exam
     */
    public String remove(Exams ex) {
        if (exDao.remove(ex) > 0) {
            showPrimeMessage("Success", "Course " + ex.getExamId() + " has been removed");
            allExam = exDao.readLiving();
        }
        return "list?faces-redirect=true";
    }

    /**
     * @return Exams
     */
    public Exams getEditExam() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        exam = (Exams) session.getAttribute("editExam");
        return exam;
    }

    /**
     * @return void
     * @see list of exams filtered by some course
     */
    public void filterByCourse() {
        Courses c = coDao.readByID(courseID);
        allExam = courseID == 0 ? allExam : Converter.iteratorToList(c.getExamses().iterator());
        showPrimeMessage("Notice", "Found: " + allExam.size() + " item(s)");
    }
}
